<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="24">
          <a-form-item label="商品名称" v-bind="validateInfos.tradeName">
            <a-input v-model:value="formData.tradeName" placeholder="请输入商品名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="商品编码" v-bind="validateInfos.commodityCode">
            <a-input v-model:value="formData.commodityCode" placeholder="请输入商品编码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="商品分类" v-bind="validateInfos.commdityClassification">
            <a-input v-model:value="formData.commdityClassification" placeholder="请输入商品分类" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="规格型号" v-bind="validateInfos.specification">
            <a-input v-model:value="formData.specification" placeholder="请输入规格型号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="供货商" v-bind="validateInfos.supplier">
            <a-input v-model:value="formData.supplier" placeholder="请输入供货商" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="商品品牌" v-bind="validateInfos.commodityBrand">
            <a-input v-model:value="formData.commodityBrand" placeholder="请输入商品品牌" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="计价单价" v-bind="validateInfos.pricingUnitPrice">
	          <a-input-number v-model:value="formData.pricingUnitPrice" placeholder="请输入计价单价" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="采购含税单价" v-bind="validateInfos.purchaseIncludTax">
	          <a-input-number v-model:value="formData.purchaseIncludTax" placeholder="请输入采购含税单价" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="采购税率" v-bind="validateInfos.purchaseTaxRate">
	          <a-input-number v-model:value="formData.purchaseTaxRate" placeholder="请输入采购税率" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="采购折扣率" v-bind="validateInfos.purchaseDiscountRate">
	          <a-input-number v-model:value="formData.purchaseDiscountRate" placeholder="请输入采购折扣率" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="销售单价" v-bind="validateInfos.salesUnitPrice">
	          <a-input-number v-model:value="formData.salesUnitPrice" placeholder="请输入销售单价" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="销售税率" v-bind="validateInfos.salesTaxRate">
	          <a-input-number v-model:value="formData.salesTaxRate" placeholder="请输入销售税率" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="销售折扣率" v-bind="validateInfos.salesDiscountRate">
	          <a-input-number v-model:value="formData.salesDiscountRate" placeholder="请输入销售折扣率" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="最低限价" v-bind="validateInfos.floorPrice">
	          <a-input-number v-model:value="formData.floorPrice" placeholder="请输入最低限价" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="商品介绍" v-bind="validateInfos.productIntroduction">
            <a-input v-model:value="formData.productIntroduction" placeholder="请输入商品介绍" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="制单人ID" v-bind="validateInfos.creatorId">
            <a-input v-model:value="formData.creatorId" placeholder="请输入制单人ID" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="制单人姓名" v-bind="validateInfos.creatorName">
            <a-input v-model:value="formData.creatorName" placeholder="请输入制单人姓名" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="是否删除" v-bind="validateInfos.isDelete">
            <a-input v-model:value="formData.isDelete" placeholder="请输入是否删除" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="供货周期" v-bind="validateInfos.supplyCycle">
            <a-input v-model:value="formData.supplyCycle" placeholder="请输入供货周期" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="单位" v-bind="validateInfos.company">
            <a-input v-model:value="formData.company" placeholder="请输入单位" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../CommodityPrice.api';
  import { Form } from 'ant-design-vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    tradeName: '',   
    commodityCode: '',   
    commdityClassification: '',   
    specification: '',   
    supplier: '',   
    commodityBrand: '',   
    pricingUnitPrice: undefined,
    purchaseIncludTax: undefined,
    purchaseTaxRate: undefined,
    purchaseDiscountRate: undefined,
    salesUnitPrice: undefined,
    salesTaxRate: undefined,
    salesDiscountRate: undefined,
    floorPrice: undefined,
    productIntroduction: '',   
    creatorId: '',   
    creatorName: '',   
    isDelete: '',   
    supplyCycle: '',   
    company: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
    tradeName: [{ required: true, message: '请输入商品名称!'},],
    commodityCode: [{ required: true, message: '请输入商品编码!'},],
    commdityClassification: [{ required: true, message: '请输入商品分类!'},],
    specification: [{ required: true, message: '请输入规格型号!'},],
    supplier: [{ required: true, message: '请输入供货商!'},],
    commodityBrand: [{ required: true, message: '请输入商品品牌!'},],
    pricingUnitPrice: [{ required: true, message: '请输入计价单价!'},],
    purchaseIncludTax: [{ required: true, message: '请输入采购含税单价!'},],
    purchaseTaxRate: [{ required: true, message: '请输入采购税率!'},],
    purchaseDiscountRate: [{ required: true, message: '请输入采购折扣率!'},],
    salesUnitPrice: [{ required: true, message: '请输入销售单价!'},],
    salesTaxRate: [{ required: true, message: '请输入销售税率!'},],
    salesDiscountRate: [{ required: true, message: '请输入销售折扣率!'},],
    floorPrice: [{ required: true, message: '请输入最低限价!'},],
    productIntroduction: [{ required: true, message: '请输入商品介绍!'},],
    creatorId: [{ required: true, message: '请输入制单人ID!'},],
    creatorName: [{ required: true, message: '请输入制单人姓名!'},],
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      //赋值
      Object.assign(formData, record);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    min-height: 500px !important;
    overflow-y: auto;
    padding: 24px 24px 24px 24px;
  }
</style>
